package asionreachel.cn.smarthomedemo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import asionreachel.cn.smarthomedemo.utils.AppUtils;


/**
 * Created by Simon(lyd) on 2016/10/22 14:52.
 * 描述：圆弧布局
 */

public class ArcLayout extends ViewGroup {

    private int Width;

    private int Height;

    private int R;

    private int centreX, centreY; //圆心

    private ArrayList<ChildSize> listSize;

    private int Margin; //该值为控件距离边距的距离

    public ArcLayout(Context context) {
        this(context, null);
    }

    public ArcLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Margin = AppUtils.dip2px(getContext(), 50);
        listSize = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Width = MeasureSpec.getSize(widthMeasureSpec);
        Height = MeasureSpec.getSize(heightMeasureSpec);
        R = Height / 2 - Margin;

        listSize.clear();
        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            this.measureChild(child, widthMeasureSpec, heightMeasureSpec);

            int cw = child.getMeasuredWidth();
            int ch = child.getMeasuredHeight();
            listSize.add(new ChildSize(cw, ch));
        }

        centreX = Width - Margin;
        centreY = Height / 2-AppUtils.dip2px(getContext(),8);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = this.getChildCount();

        int angle = 180 / (childCount - 1); //每个控件之间的距离

        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            int x = (int) (centreX + R * Math.cos((angle * i + 90) * Math.PI / 180));  //子控件中心坐标
            int y = (int) (centreY + R * Math.sin((angle * i + 90) * Math.PI / 180));  //子控件中心坐标
            int width = listSize.get(i).width / 2;    //获得控件宽度一半
            int height = listSize.get(i).height / 2;  //获得控件高度一半
            if (width == height) {  //正方形
                int left = x - width;
                int top = y - height;
                int right = x + width;
                int bottom = y + height;
                child.layout(left, top, right, bottom);
            }else{  //长方形
                int left = x - width;
                int top = y - width;
                int right = x + width;
                int bottom = y + 2*height-width;
                child.layout(left, top, right, bottom);
            }
        }
    }


    /**
     * 子控件Size
     */
    class ChildSize {
        int width;
        int height;

        public ChildSize(int w, int h) {
            width = w;
            height = h;
        }
    }
}
